package com.mzydz.shutdown;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class MainActivity extends Activity {
    DevicePolicyManager policyManager;
    ComponentName componentName;
    private TimePicker timePickerStart;
    private TimePicker timePickerEnd;
    private Button btnTimeConfirm;
    private Handler handler;
    private int startHour;
    private int startMinute;
    private int endHour;
    private int endMinute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler = new Handler();
        btnTimeConfirm = findViewById(R.id.btnTimeConfirmed);
        timePickerStart = findViewById(R.id.timePickerStart);
        timePickerStart.setIs24HourView(true);
        timePickerEnd = findViewById(R.id.timePickerEnd);
        timePickerEnd.setIs24HourView(true);

        btnTimeConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor edit = getSharedPreferences("alarm", Activity.MODE_PRIVATE).edit();
                edit.putInt("startHour", startHour);
                edit.putInt("startMinute", startMinute);
                edit.putInt("endHour", endHour);
                edit.putInt("endMinute", endMinute);
                edit.commit();

                Intent intent = new Intent(MainActivity.this, ShutdownService.class);
                startService(intent);
            }
        });

        timePickerStart.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, final int hour, final int minute) {
                startHour = hour;
                startMinute = minute;
            }
        });

        timePickerEnd.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, final int hour, final int minute) {
                endHour = hour;
                endMinute = minute;
            }
        });

        SharedPreferences sp =  getSharedPreferences("alarm", Activity.MODE_PRIVATE);
        startHour = sp.getInt("startHour", 0);
        startMinute = sp.getInt("startMinute", 0);

        endHour = sp.getInt("endHour", 0);
        endMinute = sp.getInt("endMinute", 0);

        if(startHour != 0 && startMinute != 0){
            timePickerStart.setCurrentHour(startHour);
            timePickerStart.setCurrentMinute(startMinute);
        }

        if(endHour != 0 && endMinute != 0){
            timePickerEnd.setCurrentHour(endHour);
            timePickerEnd.setCurrentMinute(endMinute);
        }

        policyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        componentName = new ComponentName(this, MyDeviceAdminReceiver.class);
        if (!policyManager.isAdminActive(componentName)) {
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
            startActivityForResult(intent, 1);
        }
    }
}
